﻿using System.Windows;

namespace Sonce.Controls
{
    public static class Helper
    {
        /// <summary>
        /// Finds the first parent element that matches type T.
        /// </summary>
        /// <typeparam name="T">Element type, must be inherited from FrameworkElement.</typeparam>
        /// <param name="element">Child element.</param>
        /// <returns>Returns parent element if found.</returns>
        public static FrameworkElement FindParent<T>(this FrameworkElement element) where T : FrameworkElement
        {
            FrameworkElement result = null;
            if (element != null && element.Parent != null)
            {
                if (element.Parent is T)
                {
                    return element.Parent as FrameworkElement;
                }
                else if (element.Parent is FrameworkElement)
                {
                    return FindParent<T>(element.Parent as FrameworkElement);
                }
            }
            return result;
        }
    }
}
